package com.startest.sms.entity.dto;

import java.io.Serializable;

public class Response<T> implements Serializable {

    private static final long serialVersionUID = 5340750045101753433L;
    private boolean success;  //调用是否成功
    private T result;       //为true，返回调用结果
    private String error;  //为false，错误信息
    private String code;  //错误代码
    private Integer total;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.success=true;     //为true才返回结果
        this.result = result;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.success=false;     //为false才返回错误信息
        this.error = error;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
        if(code.equals("200")){
            this.success = true;
        }
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    /**
     * （感觉多余）
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Response<T> ok(T data){
        Response<T> tResponse = new Response<>();
        tResponse.setResult(data);
        return tResponse;
    }

    public static <T> Response<T> ok(){
        return Response.ok(null);
    }

    public static <T> Response<T> fail(String error){
        Response<T> tResponse = new Response<>();
        tResponse.setError(error);
        return tResponse;
    }
    public static <T> Response<T> fail(String code,String error){
        Response<T> tResponse = new Response<>();
        tResponse.setError(error);
        tResponse.setCode(code);
        return tResponse;
    }
}
